package com.braveman.im.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.AddFriendsAndGroupBean;
import com.braveman.im.bean.DataListBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.EditAddGroupforActivity;
import com.braveman.im.ui.activity.message.GroupInfoActivity;
import com.braveman.im.ui.activity.message.LoginActivity;
import com.braveman.im.ui.adapter.AddFriendsAdapter;
import com.braveman.im.ui.adapter.AddGroupAdapter;
import com.braveman.im.ui.adapter.AddPlateformAdapter;
import com.braveman.im.utils.AppUtils;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class SearchResultActivity extends BaseActivity {
    private RecyclerView mLvActivityListMember;
    private List<DataListBean> groupMemberBeanList = new ArrayList<>();
    private LoadingLayout loadingLayout;
    private SwipeRefreshLayout mRefreshLayout;
    private AddFriendsAdapter addFriendsAdapter;
    private EditText mCetActivityAddFriendsSearch;
    private int mRefreshType;
    private AddGroupAdapter addGroupAdapter;
    private AddPlateformAdapter addPlateformAdapter;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected boolean isFulleScreenButAnv() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        String title = "";
        switch (mRefreshType) {
            case 0:
                title = "添加好友";
                break;
            case 1:
                title = "添加群组";
                break;
            case 2:
                title = "添加平台";
                break;
        }
        mTitleBarView.setTitleText(title).setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(SearchResultActivity.this);
            }
        });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_search_friends;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mRefreshType = callIntent.getIntExtra("mRefreshType", -1);
    }

    @Override
    protected void initChildView() {
        loadingLayout = (LoadingLayout) findViewById(R.id.loading_layout);
        mLvActivityListMember = (RecyclerView) findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        mCetActivityAddFriendsSearch = (EditText) findViewById(R.id.cet_activity_add_friends_search);
        addFriendsAdapter = new AddFriendsAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
        addGroupAdapter = new AddGroupAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
        addPlateformAdapter = new AddPlateformAdapter(R.layout.list_add_friends_item, groupMemberBeanList);

        mLvActivityListMember.setAdapter(addFriendsAdapter);
        loadingLayout.showEmpty();
        switch (mRefreshType) {
            case 0:
                mCetActivityAddFriendsSearch.setHint("请输入手机号或诚信号");
                mCetActivityAddFriendsSearch.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 1:
                mCetActivityAddFriendsSearch.setHint("请输入诚信群号或诚信群名称");
                mCetActivityAddFriendsSearch.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 2:
                mCetActivityAddFriendsSearch.setHint("请输入平台诚信号或平台名称");
                mCetActivityAddFriendsSearch.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
        }
    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {

        mCetActivityAddFriendsSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == 3) {
                /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                    }

                    doNetwork(mRefreshType);
                    return true;
                }
                return false;
            }
        });

        addFriendsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                switch (mRefreshType) {
                    case 0:
                        bundle.putString("friendId", groupMemberBeanList.get(position).id);
                        ActivityUtils.startActivity(SearchResultActivity.this, PersonInfoDetailsActivirt.class, bundle);
                        break;
                    case 1:
                        bundle.putString("groupId", groupMemberBeanList.get(position).id);
                        ActivityUtils.startActivity(SearchResultActivity.this, GroupInfoActivity.class, bundle);
                        break;
                }

            }
        });

        addGroupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("groupId", groupMemberBeanList.get(position).id);
                bundle.putString("groupName", groupMemberBeanList.get(position).groupName);
                ActivityUtils.startActivity(SearchResultActivity.this, EditAddGroupforActivity.class, bundle);
            }
        });

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doNetwork(mRefreshType);
            }
        });
        findViewById(R.id.tv_activity_search_back);
    }

    private void doNetwork(int mRefreshType) {
        String search = mCetActivityAddFriendsSearch.getText().toString().trim();
        loadingLayout.showLoading();
        switch (mRefreshType) {
            case 0:
                ApiManager.getApi().searchFriendsList(search, false, 1, 10).enqueue(new NetCallBack<AddFriendsAndGroupBean>() {
                    @Override
                    protected void onSuccess(Call<AddFriendsAndGroupBean> call, Response<AddFriendsAndGroupBean> response) {
                        if (response != null) {
                            AddFriendsAndGroupBean groupMemberBean = response.body();

                            if (groupMemberBeanList != null) {
                                groupMemberBeanList.clear();
                            }
                            groupMemberBeanList.addAll(groupMemberBean.dataList);
                            mLvActivityListMember.setAdapter(addFriendsAdapter);
                            if (addFriendsAdapter != null) {
                                addFriendsAdapter.notifyDataSetChanged();
                            }

                            if (groupMemberBeanList.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                            mRefreshLayout.setRefreshing(false);
                        } else {

                            loadingLayout.showError();
                            mRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<AddFriendsAndGroupBean> call, Throwable t) {
                        super.onFailure(call, t);
                        loadingLayout.showError();
                        mRefreshLayout.setRefreshing(false);
                    }
                });
                break;
            case 1://感兴趣的群
                ApiManager.getApi().searchGroupList(search, false, 1, 10).enqueue(new NetCallBack<AddFriendsAndGroupBean>() {
                    @Override
                    protected void onSuccess(Call<AddFriendsAndGroupBean> call, Response<AddFriendsAndGroupBean> response) {
                        if (response != null) {
                            AddFriendsAndGroupBean groupMemberBean = response.body();

                            if (groupMemberBeanList != null) {
                                groupMemberBeanList.clear();

                            }
                            groupMemberBeanList.addAll(groupMemberBean.dataList);
                            mLvActivityListMember.setAdapter(addGroupAdapter);
                            if (addFriendsAdapter != null) {
                                addFriendsAdapter.notifyDataSetChanged();
                            }

                            if (groupMemberBeanList.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                            mRefreshLayout.setRefreshing(false);
                        } else {

                            loadingLayout.showError();
                            mRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<AddFriendsAndGroupBean> call, Throwable t) {
                        super.onFailure(call, t);
                        loadingLayout.showError();
                        mRefreshLayout.setRefreshing(false);
                    }
                });
                break;
            case 2://感兴趣的平台
                ApiManager.getApi().searchPaltformList(search, false, 1, 10).enqueue(new NetCallBack<AddFriendsAndGroupBean>() {
                    @Override
                    protected void onSuccess(Call<AddFriendsAndGroupBean> call, Response<AddFriendsAndGroupBean> response) {
                        if (response != null) {
                            AddFriendsAndGroupBean groupMemberBean = response.body();

                            if (groupMemberBeanList != null) {
                                groupMemberBeanList.clear();

                            }
                            groupMemberBeanList.addAll(groupMemberBean.dataList);
                            mLvActivityListMember.setAdapter(addPlateformAdapter);
                            if (addFriendsAdapter != null) {
                                addFriendsAdapter.notifyDataSetChanged();
                            }

                            if (groupMemberBeanList.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                            mRefreshLayout.setRefreshing(false);
                        } else {

                            loadingLayout.showError();
                            mRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<AddFriendsAndGroupBean> call, Throwable t) {
                        super.onFailure(call, t);
                        loadingLayout.showError();
                        mRefreshLayout.setRefreshing(false);
                    }
                });
                break;
        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_activity_search_back:
                ActivityUtils.finishActivity(SearchResultActivity.this);
                break;
        }
    }
}
